package com.btm.distrb.config;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * mysql数据库相关配置类
 */
public class MysqlConfig {

    private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
    private static String url = "";
    private static String userName = "";
    private static String password = "";

    static {
        try {
            Class.forName(DRIVER);
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }
    }

    /**
     * 获得数据库连接
     * @return
     * @throws SQLException
     */
    public Connection getConnection() throws SQLException{
        return DriverManager.getConnection(url,userName,password);
    }

    /**
     * 关闭数据库连接
     * @param connection
     * @throws SQLException
     */
    public void closeConnect(Connection connection) throws SQLException{
        if (null != connection && !connection.isClosed()){
            connection.close();
        }
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
